﻿Imports DTO
Imports System.Data.OleDb

Public Class KhachHangDAO
    Public Shared Function SelectKhachHangLikeTen(ByVal kh As KhachHangDTO) As DataTable
        Dim sql As String = "select * from KHACHHANG where HoTenKhachHang like '%" & kh.HoTenKhachHang & "%'"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Function SelectKhachHangLikeDiaChi(ByVal kh As KhachHangDTO) As DataTable
        Dim sql As String = "select * from KHACHHANG where DiaChi like '%" & kh.DiaChi & "%'"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Function SelectKhachHangLikeDienThoai(ByVal kh As KhachHangDTO) As DataTable
        Dim sql As String = "select * from KHACHHANG where DienThoai like '%" & kh.DienThoai & "%'"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Function SelectKhachHangLikeEmail(ByVal kh As KhachHangDTO) As DataTable
        Dim sql As String = "select * from KHACHHANG where Email like '%" & kh.Email & "%'"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Function GetKhachHangAll() As DataTable
        Dim sql As String = "select * from KHACHHANG"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Sub Insert(ByVal kh As KhachHangDTO)
        Dim sql As String = "insert into KHACHHANG(HoTenKhachHang,DienThoai,DiaChi,Email) values('" & kh.HoTenKhachHang & "'," & kh.DienThoai & ",'" & kh.DiaChi & "','" & kh.Email & "')"
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Sub Update(ByVal kh As KhachHangDTO)
        Dim sql As String = "Update  KHACHHANG set HoTenKhachHang =('" & kh.HoTenKhachHang & "'),DienThoai=(" & kh.DienThoai & "), DiaChi=('" & kh.DiaChi & "'),Email=('" & kh.Email & "') where MaKhachHang=" & kh.MaKhachHang & ""
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Sub UpdateTienNo(ByVal kh As KhachHangDTO)
        Dim sql As String = "Update  KHACHHANG set TienNo=(" & kh.TienNo & ") where MaKhachHang=" & kh.MaKhachHang & ""
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Function Delete(ByVal kh As KhachHangDTO) As Boolean
        Try
            Dim sql As String = "delete from KHACHHANG where MaKhachHang= " & kh.MaKhachHang & ""
            DatabassProvider.ExcuNonQuery(sql)
            Return True
        Catch
            Return False
        End Try
    End Function

    Public Shared Function GetTheLoaiByMa(ByVal Ma As Integer) As KhachHangDTO
        Dim sql As String = "select * from KHACHHANG where MaKhachHang=" & Ma & ""
        Dim dt As DataTable = DatabassProvider.ExcuQuery(sql)
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Dim kh As New KhachHangDTO()
            kh.MaKhachHang = CInt(Fix(dt.Rows(0).ItemArray(0)))
            kh.HoTenKhachHang = dt.Rows(0).ItemArray(1).ToString()
            kh.DienThoai = CInt(Fix(dt.Rows(0).ItemArray(2)))
            kh.DiaChi = dt.Rows(0).ItemArray(3).ToString()
            kh.Email = dt.Rows(0).ItemArray(4).ToString()
            Return kh
        End If

    End Function
End Class
